using UnityEngine;
using UnityEngine.UI;

namespace JH
{
    /// <summary>
    ///  折线图区域渲染器
    /// </summary>
    public class LinAreaRender : BasicGraphicRender<Vector2>
    {
        /// <summary>
        ///  线渲染器
        /// </summary>
        private Vector2LineRender _lineRender;

        /// <summary>
        ///  获取线渲染器
        /// </summary>
        private Vector2LineRender LineRender => _lineRender ??= this.GetChartComp<Vector2LineRender, Vector2>();


        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();
            if (LineRender.NoWidthVertexList.Count < 2) return;

            // 逐个数据点处理
            for (var i = 0; i < LineRender.NoWidthVertexList.Count; i++)
            {
                // 当前数据点
                var each = LineRender.NoWidthVertexList[i];

                // 原始顶点
                UIVertex vertex = UIVertex.simpleVert;
                vertex.position = each.OSPosition;
                vertex.color = color;
                vertex.uv0 = new Vector2(each.UV.x, 1);

                // 添加原始顶点到顶点列表
                vh.AddVert(vertex);

                // 偏移顶点
                var offsetVert = vertex;
                offsetVert.uv0 = new Vector2(vertex.uv0.x, 0);
                offsetVert.position = each.BoundaryHandler.GetOSPositionByUV(offsetVert.uv0);

                // 添加偏移顶点到顶点列表
                vh.AddVert(offsetVert);

                // 添加索引
                if (i > 0)
                {
                    var a = (i - 1) * 2;
                    var b = (i - 1) * 2 + 1;
                    var c = i * 2;
                    var d = i * 2 + 1;

                    // 添加三角形索引
                    vh.AddTriangle(b, a, c);
                    vh.AddTriangle(c, b, d);
                }
            }
        }

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            base.OnValidate();
            Priority = 1;
        }
#endif
    }
}